package eatwatch;

import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

class FormDailyReport extends Form implements CommandListener, ItemCommandListener
{
	private 		EatWatch		mMIDlet 		= null;
	private 		Displayable	mPrevScreen		= null;
	private final   StringItem	mConsumed		= new StringItem ("Consumate:", "0");
	private final   StringItem	mRemaining		= new StringItem ("Ramase:", "0");
	private	final	IDatabase	mDatabase   	= EatWatch.mDatabase;
	private final	Command 	cmdBack  		= new Command ("Inapoi", Command.BACK, 0);
	private final	Command 	cmdUpdate  		= new Command ("Modifica", Command.OK, 0);
	private final	Command 	cmdDelete  		= new Command ("Sterge", Command.OK, 1);
	private 		ChoiceGroup	chgIngredient	= new ChoiceGroup ("", ChoiceGroup.EXCLUSIVE);
	private 		Hashtable   mIndex			= new Hashtable ();
	private 		FormDailyIntake	mDailyIntake	= null;

	public FormDailyReport (EatWatch midlet)
	{
		super ("Raport azi");
		mMIDlet = midlet;
		append (mConsumed);
		append (mRemaining);
		append (new Spacer (10, 3));
		append (chgIngredient);

		addCommand (cmdBack);
		addCommand (cmdUpdate);
		addCommand (cmdDelete);
		setCommandListener (this);
		mDailyIntake  = mMIDlet.mDailyIntake;
	}

	public void commandAction(Command c, Displayable d)
	{
		if (c == cmdBack)
			Display.getDisplay (mMIDlet).setCurrent (mPrevScreen);
		else if (c == cmdUpdate) {
			Date date = (Date)mIndex.get (new Integer(chgIngredient.getSelectedIndex ()));
			mDailyIntake.ShowUpdate (this, date);
		}
		else if (c == cmdDelete) {
			Date date = (Date)mIndex.get (new Integer(chgIngredient.getSelectedIndex ()));
			DeleteCurrent (date);
			PrepareForm ();
		}
	}


	public void commandAction(Command c, Item item)
	{
	}

	private void PrepareForm ()
	{
		float consumed = 0;

		mDatabase.Open ("intake");
		Record rec = null;
		Calendar currentDate = Calendar.getInstance ();
		Calendar recordDate  = Calendar.getInstance ();
		chgIngredient.deleteAll ();
		int idx = 0;
		while ((rec = mDatabase.ReadRecord ()) != null) {
			Date recDate = (Date)rec.GetField ("date");
			mIndex.put (new Integer(idx++), recDate);
			recordDate.setTime(recDate);

			if (currentDate.get (Calendar.DATE)  != recordDate.get (Calendar.DATE) ||
			    currentDate.get (Calendar.MONTH) != recordDate.get (Calendar.MONTH) ||
			    currentDate.get (Calendar.YEAR)  != recordDate.get (Calendar.YEAR))
			    continue;

			float calories = ((Integer)rec.GetField ("calories")).intValue ();
			float divideBy = ((Integer)rec.GetField ("MU")).intValue () == 0 ? 100 : 1;
			float quantity = ((Integer)rec.GetField ("quantity")).intValue ();
			float total = calories * quantity / divideBy;
			consumed += total;
			chgIngredient.append ((rec.GetField ("ingredient") + ": " + total), null);
		}
		mConsumed.setText ("" + consumed);
		mRemaining.setText ("" + (1700 - consumed));
		mDatabase.Close ();
	}

	public void Show (Displayable prevForm)
	{
		mPrevScreen = prevForm;
		Show ();
	}

	public void Show ()
	{
		PrepareForm ();
		Display.getDisplay (mMIDlet).setCurrent (this);
	}

	private void DeleteCurrent (Date date)
	{
		mDatabase.Open ("intake");
		Record rec = mDatabase.FindRecord ("date", date);
		mDatabase.DeleteRecord (rec.GetId ());
		mDatabase.Close ();
	}
}
